<?php
/**
 * Created by 绵阳人维网络科技有限公司.
 * User: 罗东华
 * Date: 2016/3/13
 * Time: 15:40
 */
namespace Admin\Model;

class AdminModel extends AdminModelBase{
    //加密钥匙  不能改
    private $ADMIN_KEY='ejoanioe&435&^#23';


    /*管理员信息*/
    public function admin_info(){
        $ldg_userid=session('ldh_userid');
        if($ldg_userid){
            $where['id']=$this->key_decode($ldg_userid,$this->ADMIN_KEY);
            $res=$this->where($where)->field('usermd5,password',true)->cache(true)->find();
        }
         return $res;

    }
    /*登陆检查*/
    public function admin_count($data){
        $where['usermd5']=$this->Md5_password($data['username']);
        $where['password']=$this->Md5_password($data['password']);

        $id= $this->where($where)->getField('id');
        if($id){
            session(null);
            session('ldh_userid',$this->key_encode($id,$this->ADMIN_KEY));
            return true;
        }else{
            return false;
        }
    }


    public function Md5_password($password){
          return md5($password.$this->ADMIN_KEY);
    }
    /*检查密码正确*/
    public function re_psd($password){
        $where['id']=\Admin\Controller\AdminBase::$userinfo['id'];
        $where['password']=$this->Md5_password($password);
        if($this->where($where)->count()>0){
            return true;
        }else{
            return false;
        }

    }
    /*修改管理员密码*/
    public function saveuser($where,$field){

        $where['password']=$this->Md5_password($where['password']);
        $where['id']=\Admin\Controller\AdminBase::$userinfo['id'];
        $field['password']=$this->Md5_password($field['password']);
        $field['usermd5']=$this->Md5_password($field['username']);
        if($this->where($where)->save($field)){
            return true;
        }else{
            return false;
        }

    }


    /**
     * 简单对称加密算法之加密
     * @param String $string 需要加密的字串
     * @param String $skey 加密EKY
    */
    private function key_encode($string = '', $skey = 'cxphp') {
        $strArr = str_split(base64_encode($string));
        $strCount = count($strArr);
        foreach (str_split($skey) as $key => $value)
            $key < $strCount && $strArr[$key].=$value;
        return str_replace(array('=', '+', '/'), array('O0O0O', 'o000o', 'oo00o'), join('', $strArr));
    }
    /**
     * 简单对称加密算法之解密
     * @param String $string 需要解密的字串
     * @param String $skey 解密KEY
     */

    private  function key_decode($string = '', $skey = 'cxphp') {
        $strArr = str_split(str_replace(array('O0O0O', 'o000o', 'oo00o'), array('=', '+', '/'), $string), 2);
        $strCount = count($strArr);
        foreach (str_split($skey) as $key => $value)
            $key <= $strCount  && isset($strArr[$key]) && $strArr[$key][1] === $value && $strArr[$key] = $strArr[$key][0];
        return base64_decode(join('', $strArr));
    }

}